<template>
  <div class="article-page">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>面经后台</el-breadcrumb-item>
      <el-breadcrumb-item>面经管理</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card shadow="never" border="false">
      <template #header>
        <div class="header">
          <span>共 300 条记录</span>
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            round
            @click="onShow('add')"
          >
            添加面经
          </el-button>
        </div>
      </template>
      <el-table :data="list">
        <el-table-column label="作者" prop="stem"></el-table-column>
        <el-table-column label="点赞" prop="likeCount"></el-table-column>
        <el-table-column label="浏览" prop="views"></el-table-column>
        <el-table-column label="更新时间" prop="createdAt"></el-table-column>

        <el-table-column label="操作" width="120px">
          <template #default="{ row }">
            <div class="tab">
              <i class="el-icon-view" @click="onShow('preview', row.id)"></i>
              <i class="el-icon-edit-outline" @click="onShow('edit', row.id)">
              </i>
              <!-- 添加点击事件 删除元素 -->
              <i class="el-icon-delete" @click="onDel(row.id)"></i>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        layout="prev,pager,next"
        :total="total"
        :current-page="current"
        @current-change="changePage"
      />
    </el-card>

    <!-- 添加面经管理 -->
    <el-drawer :title="title" :visible.sync="drawer" size="50%">
      <!-- 使用v-if和v-html创建预览页面 -->
      <div v-if="type === 'preview'" class="article-preview">
        <h5>{{ onFrom.stem }}</h5>
        <div v-html="onFrom.content"></div>
      </div>
      <!-- 绑定ref 单独校验content失焦事件 -->
      <el-form label-width="80px" :model="onFrom" :rules="rules" ref="formRef">
        <el-form-item label="标题" prop="stem">
          <el-input v-model="onFrom.stem"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <!-- 编辑框 -->
          <quill-editor v-model="onFrom.content" @blur="onContentBlur">
          </quill-editor>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button @click="drawer = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script>
// require styles
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'

import { quillEditor } from 'vue-quill-editor'
import {
  addArticleApi,
  delArticleApi,
  getDateArticleApi,
  getListInfoApi,
  updateArticleApi
} from '@/api/article'
export default {
  components: {
    quillEditor
  },
  name: 'article-page',
  // 给表单做非空校验 双向绑定数据 定义规则
  data () {
    return {
      list: [],
      // 定义页码
      current: 1,
      // 定义总页数
      total: 1,
      drawer: false,
      type: 'add', // 用来定义新增，修改和预览,
      // 设置非空校验数据
      onFrom: {
        stem: '',
        content: ''
      },
      // 定义规则
      rules: {
        stem: [{ required: true, message: '请输入标题内容', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'change' }]
      }
    }
  },
  created () {
    // 打开页面调用接口
    this.onLoad()
  },
  methods: {
    // 需求：实现编辑功能//1。查询最新的详情数据，封装查询详情API//2。封装更新数据的接口//3。点击事件，打开弹出层//4。发请求查询详情数据//5。保存数据到formData
    // 点击删除按钮，再次确认删除信息 调用封装的删除api请求，删除元素  再次调用查询信息，刷新页面 删除成功提示
    async onDel (id) {
      await this.$confirm('确认要删除吗？', '温馨提示')
      await delArticleApi(id)
      this.onLoad()
      this.$message.success('删除成功')
    },
    //  点击确认按钮 做表单校验 封装API 调用API 提示用户 重置页面 调用刷新接口  判断是编辑还是新增
    async onSubmit () {
      if (this.type === 'add') {
        await this.$refs.formRef.validate()
        await addArticleApi(this.onFrom)
        this.$message.success('新增成功')
      }

      if (this.type === 'edit') {
        await this.$refs.formRef.validate()
        const res = await updateArticleApi(this.onFrom)
        console.log(res)
        this.$message.success('编辑成功')
      }
      this.current = 1
      this.onLoad()
      this.drawer = false
    },
    // 封装一个api函数
    async onLoad () {
      const res = await getListInfoApi(this.current)
      // console.log(res)
      this.list = res.data.rows
      this.current = res.data.current
      this.total = res.data.total
    },
    changePage (xxx) {
      this.current = xxx
      console.log(xxx)
      // 再次调用接口
      this.onLoad()
    },
    async onShow (type, id) {
      // 根据点击的参数显示，判断是否是编辑或者预览 发送查询接口 回填数据 并且打开显示框

      if (type !== 'add') {
        const res = await getDateArticleApi(id)
        // console.log(res)
        this.onFrom = {
          stem: res.data.stem,
          content: res.data.content,
          // 保存id  为了编辑成功使用
          id: res.data.id
        }
      }
      this.type = type
      this.drawer = true
    },
    // 给单独校验的内容失焦事件方法设置
    onContentBlur () {
      this.$refs.formRef.validateField('content')
    }
  },
  computed: {
    title () {
      // 判断点击的是什么，显示响应的数据
      if (this.type === 'add') return '新增面经'
      if (this.type === 'edit') return '编辑面经'
      return '预览面经'
    }
  },
  // 1，定义计算属性 监听drawer变为false时，重置表单
  watch: {
    drawer (newValue) {
      if (!newValue) {
        // 判注意点: 由于公用的抽屉, 当预览时, 是没有表单的! 不能重置表单
        if (this.type !== 'preview') {
          this.$refs.formRef.resetFields()
        }
        // this.type === 'preview' && this.$refs.formRef.resetFields()
        // 手动重置表单
        this.onFrom = {
          stem: '',
          content: ''
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.tab {
  display: flex;
  justify-content: space-between;
}
.el-card {
  margin-top: 25px;
  .header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-right: 16px;
  }
  .actions {
    font-size: 18px;
    display: flex;
    justify-content: space-around;
    color: #666;
    > i:hover {
      color: rgba(114, 124, 245, 1);
      cursor: pointer;
    }
  }
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-breadcrumb {
  margin-top: 10px;
}
.el-form {
  padding-right: 40px;
}
.quill-editor {
  ::v-deep .ql-editor {
    height: 300px;
  }
}
.el-rate {
  padding: 10px 0;
}
.article-preview {
  padding: 0 40px 40px 40px;
  > h5 {
    font-size: 20px;
    color: #666;
    border-bottom: 1px dashed #ccc;
    padding-bottom: 30px;
    margin: 0 0 20px 0;
  }
}
</style>
